/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.platform.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springblade.modules.platform.entity.DeviceTable;
import org.springblade.modules.platform.entity.EquipmentLabel;
import org.springblade.modules.platform.entity.ProductModelLabel;
import org.springblade.modules.platform.service.IDeviceTableService;
import org.springblade.modules.platform.service.IEquipmentLabelService;
import org.springblade.modules.platform.service.IProductModelLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2020-12-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/service-huateng-sxdb/equipmentlabel")
@Api(value = "", tags = "设备添加标签接口")
public class EquipmentLabelController extends BladeController {
	private final IProductModelLabelService productModelLabelService;
	private final IEquipmentLabelService equipmentLabelService;
	private final IDeviceTableService iDeviceTableService;


	/**
	 * 分页
	 */
	@ApiLog("设备管理-标签-列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入equipmentId")
	public R<List<EquipmentLabel>> list(String equipmentId) {
		List<EquipmentLabel> list = equipmentLabelService.list(Wrappers.<EquipmentLabel>query().lambda().eq(EquipmentLabel::getEquipmentId, equipmentId));
		return R.data(list);
	}


	/**
	 * 新增
	 */
	@ApiLog("设备管理-标签-新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入equipmentLabel")
	public R save(@Valid @RequestBody EquipmentLabel equipmentLabel) {
		equipmentLabel.setInsertTime(System.currentTimeMillis());
		equipmentLabel.setUpdateTime(System.currentTimeMillis());
		return R.status(equipmentLabelService.save(equipmentLabel));
	}

	/**
	 * 修改
	 */
	@ApiLog("设备管理-标签-修改")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入equipmentLabel")
	public R update(@Valid @RequestBody EquipmentLabel equipmentLabel) {
		equipmentLabel.setUpdateTime(System.currentTimeMillis());
		return R.status(equipmentLabelService.updateById(equipmentLabel));
	}



	/**
	 * 删除
	 */
	@ApiLog("设备管理-标签-删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(equipmentLabelService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 分页
	 */
	@ApiLog("设备管理-标签-列表")
	@GetMapping("/getListLabel")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "下拉框", notes = "传入equipmentId")
	public R<JSONArray> getListLabel(String equipmentId) {
		DeviceTable detail = iDeviceTableService.getOne(new QueryWrapper<DeviceTable>().lambda()
			.eq(DeviceTable::getDeviceId, equipmentId)
			.eq(DeviceTable::getIsDeleted,0));
		ProductModelLabel one = productModelLabelService.getOne(Wrappers.<ProductModelLabel>query().lambda().eq(ProductModelLabel::getProductId, detail.getProductId()));
		if(one==null){
			return R.data(new JSONArray());
		}
		String labelJson = one.getLabelJson();
		JSONArray jsonArray = JSONArray.parseArray(labelJson);
		return R.data(jsonArray);
	}

}
